﻿using Inovout.Cloud.DataStore.Common;
using Inovout.Cloud.DataStore.Services;
using Inovout.Cloud.DataStore.Utility;
using Inovout.Runtime;
using Inovout.Web;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

[assembly: log4net.Config.DOMConfigurator(Watch = true)]
namespace Inovout.Cloud.DataStore.Web
{
    public class WebApiApplication : WebApplication
    {
        private Inovout.Security.User user = null;
        private Inovout.Memberships.Models.Membership membership = null;
        public override void Run()
        {
            base.Run();

            //Start a thread to count total records in the mongodb and save the result into collection all the time.
            Thread thread = new Thread(new ThreadStart(RecordsSave));
            thread.Start();

        }

        /// <summary>
        /// Count total records 
        /// </summary>
        private long RecordsCount(MongoDbClient mongodbClient)
        {
            long recordsCount = 0;
            var databaseList = mongodbClient.GetDatabaseNames();
            foreach (var db in databaseList)
            {
                if (db != "admin" && db != "config")
                {
                    var tableNames = mongodbClient.GetTableNames(db);
                    foreach (var tableName in tableNames)
                    {
                        recordsCount += mongodbClient.GetCount(db, tableName);
                    }
                }
            }
            return recordsCount;
        }

        private const string totalRecordsDatabaseName = "TotalRecordsDatabaseName";
        private const string totalRecordsTableName = "TotalRecordsTableName";
        private const string totalRecordsPrimaryKey = "TotalRecordsPrimaryKey";

        private void RecordsSave()
        {
            var mongodbClient = ComponentRegistry.Resolve<EntityService>().MongoDbClient;
            string databaseName = null;
            string tableName = null;
            string primaryKey = null;
            GetTotalRecordsInfo(ref databaseName, ref tableName, ref primaryKey);
            while (true)
            {
                long result = RecordsCount(mongodbClient);
                Dictionary<string, object> rowEntity = new Dictionary<string, object>();
                if (!rowEntity.ContainsKey(Constant.ObjectId))
                {
                    rowEntity.Add(Constant.ObjectId, primaryKey);
                }
                rowEntity.Add("CountValue", result);
                if (rowEntity.Count > 0)
                {
                    mongodbClient.SaveEntity(databaseName, tableName, rowEntity);
                }
            }
        }

        private static void GetTotalRecordsInfo(ref string databaseName, ref string tableName, ref string primaryKey)
        {

            IApplication application = ComponentRegistry.Resolve<IApplication>();
            if (application.Settings.Contains(totalRecordsDatabaseName))
            {
                databaseName = application.Settings[totalRecordsDatabaseName].ToString();
            }
            else
            {
                databaseName = "3Cloud";
            }
            if (application.Settings.Contains(totalRecordsTableName))
            {
                tableName = application.Settings[totalRecordsTableName].ToString();
            }
            else
            {
                tableName = "Records_TotalRocords";
            }
            if (application.Settings.Contains(totalRecordsPrimaryKey))
            {
                primaryKey = application.Settings[totalRecordsPrimaryKey].ToString();
            }
            else
            {
                primaryKey = "TotalRecords";
            }
        }


    }
}
